package com.ejlchina.tool.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.ItemScanDeviceBinding
import com.ejlchina.tool.bean.BleDeviceBean
import com.huyuhui.fastble.BleManager
import com.huyuhui.fastble.callback.BleRssiCallback
import com.huyuhui.fastble.data.BleDevice
import com.huyuhui.fastble.exception.BleException

class ScanDeviceAdapter(private val deviceList: MutableList<BleDeviceBean>, private val connectClick: (BleDeviceBean) -> Unit, private val lightClick: (BleDeviceBean) -> Unit): RecyclerView.Adapter<ScanDeviceViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScanDeviceViewHolder {
        val binding = ItemScanDeviceBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ScanDeviceViewHolder(binding,connectClick,lightClick)
    }

    override fun getItemCount(): Int {
        return deviceList.size
    }

    override fun onBindViewHolder(holder: ScanDeviceViewHolder, position: Int) {
        holder.bindData(deviceList[position])
    }
}

class ScanDeviceViewHolder(private val binding: ItemScanDeviceBinding, private val connectClick: (BleDeviceBean) -> Unit, private val lightClick: (BleDeviceBean) -> Unit): RecyclerView.ViewHolder(binding.root){
    @SuppressLint("MissingPermission", "SetTextI18n")
    fun bindData(scanDeviceBean: BleDeviceBean){
        binding.tvMac.text = scanDeviceBean.bleDevice.device.address
        if (scanDeviceBean.isConnect){
                if (scanDeviceBean.deviceNo.isNotEmpty()){
                    binding.tvVersion.text = "设备编号：${scanDeviceBean.deviceNo}"
                    binding.tvVersion.visibility = View.VISIBLE
                }else {
                    binding.tvVersion.visibility = View.GONE
                }
        } else {
            binding.tvVersion.visibility = View.GONE
        }
        binding.tvRssi.setImageResource(setRssiIcon(scanDeviceBean.rssi))
        binding.tvVersion.text = if (scanDeviceBean.isConnect && scanDeviceBean.deviceNo.isNotEmpty()) "设备编号：${scanDeviceBean.deviceNo}" else ""
        binding.btnCollect.text = if (scanDeviceBean.isConnect) "断开" else "连接"
        binding.btnLight.visibility = if (scanDeviceBean.isConnect) View.VISIBLE else View.GONE
        binding.btnLight.text = if (scanDeviceBean.isOpenLight) "关灯" else "亮灯"
        setBtnConnectBg(scanDeviceBean.isConnect)
        binding.btnCollect.setOnClickListener {
            connectClick(scanDeviceBean)
        }

        binding.btnLight.setOnClickListener {
            lightClick(scanDeviceBean)
        }
    }

    private fun setRssiIcon(rssi: Int): Int{
        return when(rssi){
            in (-80 ..0) -> R.drawable.icon_signal_3
            in (-90 until -80) -> R.drawable.icon_signal_2
            in (-100 until -90) -> R.drawable.icon_signal_1
            else -> R.drawable.icon_signal_0
        }
    }

    private fun setBtnConnectBg(isConnect: Boolean){
        val backgroundC = if (isConnect) R.drawable.shape_co_13_so_f7f7f7 else R.drawable.shape_co_13_so_1f39ff
        binding.btnCollect.setBackgroundResource(backgroundC)
        val text = if (isConnect) "断开" else "连接"
        binding.btnCollect.text = text
        val textColor = if (isConnect) "#2C2C2E" else "#FFFFFF"
        binding.btnCollect.setTextColor(Color.parseColor(textColor))
    }
}